<#import "xml.ftl" as xml/>

<#-- Templates for rendering different violations -->
<#-- Render a violation -->
<#macro render violation locationText="">
	<#switch violation.class.simpleName>
		<#case "DuplicateElementValidationViolation">
			<@duplicateElementValidationViolation violation, locationText/>
			<#break>
		<#case "MissingElementValidationViolation">
			<@missingElementValidationViolation violation, locationText/>
			<#break>
		<#case "MissingResourceValidationViolation">
			<@missingResourceValidationViolation violation, locationText/>
			<#break>
		<#case "PageNumberValidationViolation">
			<@pageNumberValidationViolation violation, locationText/>
			<#break>
		<#case "XMLValidationViolation">
			<@xmlValidationViolation violation, locationText/>
			<#break>
		<#default>
			<li>${violation}</li>
	</#switch>
</#macro>

<#-- Display a suggested icon which expands to show a tip -->
<#macro suggested id>
	<#if id?is_number>
		<#assign newid=id?c/>
	<#else>
		<#assign newid=id/>
	</#if>
	<br/><span id="${newid}_button" class="button"
		onclick="$('${newid}_tip').toggle(); $('${newid}_button').toggle()">
		Suggested Fix
	</span>
	<div style="display:none" class="contentbox" id="${newid}_tip"
		onclick="$('${newid}_tip').toggle(); $('${newid}_button').toggle()">
		<#nested/>
	</div>
</#macro>

<#-- Display a location icon which expands to show the location -->
<#macro location id locationText>
    <#if locationText?? && (locationText?length > 0)>
    	<#if id?is_number>
			<#assign newid=id?c/>
		<#else>
			<#assign newid=id/>
		</#if>
		<span id="${newid}_l_button" class="button"
			onclick="$('${newid}_loc').toggle(); $('${newid}_l_button').toggle()">
			Problem Location
		</span>
		<div style="display:none" class="contentbox" id="${newid}_loc"
			onclick="$('${newid}_loc').toggle(); $('${newid}_l_button').toggle()">
			${locationText}
		</div>
	</#if>
</#macro>

<#-- Display a preview of the code area which has problems;
	resourcePath must be in the data model -->
<#macro preview id previewContext>
	<span id="${newid}_p_button" class="button"
		onclick="$('${newid}_preview').toggle(); $('${newid}_p_button').toggle()">
		Show Problem
	</span>
	<div style="display:none" class="contentbox" id="${newid}_preview"
		onclick="$('${newid}_preview').toggle(); $('${newid}_p_button').toggle()">
		<#assign shade = false/>
		<table class="sourcetable">
			<#list previewContext.linesBefore as line>
				<tr <#if shade>class="shaded"</#if>>
					<td>${line.getKey()}</td><td><td>${line.getValue()}</td>
				</tr>
				<#assign shade = !shade/>
			</#list>
			<tr <#if shade>class="shaded"</#if>><td><em>${previewContext.lineNumber}</em></td>
				<td><img src="${resourcePath}/exclaim.png"/>&nbsp;&nbsp;</td>
				<td><em>${previewContext.line}</em></td>
				<#assign shade = !shade/>
			</tr>
			<#list previewContext.linesAfter as line>
				<tr <#if shade>class="shaded"</#if>>
					<td>${line.getKey()}</td><td></td><td>${line.getValue()}</td>
				</tr>
				<#assign shade = !shade/>
			</#list>
		</table>
	</div>
</#macro>

<#macro duplicateElementValidationViolation violation locationText>
	<li class="${violation.severity}">Duplicate Element:
		<#if violation.attributeName??>
			Multiple elements named <em>${violation.elementName}</em> with an
			attribute <em>${violation.attributeName}="${violation.attributeValue}"</em>
			were found within the containing element <em>${violation.parentName}</em>.
		<#else>
			Multiple elements named <em>${violation.elementName}</em> were found
			within the containing element <em>${violation.parentName}</em>.
		</#if>
		The NIMAS specifications do not allow for this duplication.
		<#assign theId>${violation.hashCode()?c + random.nextInt()?c}</#assign>
		<@suggested theId>Remove one of the duplicate elements.</@suggested>
		<@location theId, locationText/>
	</li>
</#macro>

<#macro missingElementValidationViolation violation locationText>
	<li class="${violation.severity}">Missing Element:
		<#if violation.attributeName??>
			An element named <em>${violation.elementName}</em> with an
			attribute <em>${violation.attributeName}="${violation.attributeValue}"</em>
			was expected within the containing element <em>${violation.parentName}</em>.
		<#else>
			An element named <em>${violation.elementName}</em> was expected
			within the containing element <em>${violation.parentName}</em>.
		</#if>
		<@suggested violation.hashCode()>Add the element <em>${violation.elementName}</em>
		as described above. If it appears to be there already, verify the spelling,
		nesting of elements, attributes, and namespace.</@suggested>
		<@location violation.hashCode(), locationText/>
	</li>
</#macro>

<#macro missingResourceValidationViolation violation locationText>
	<li class="${violation.severity}">Missing Resource: <em>${violation.resourceName}</em>,
	as referred to within <em>${violation.context}</em>, was not found.
	<@suggested violation.hashCode()>
		Alternatives:
		<ul class="noicon">
			<li>Verify that <em>${violation.resourceName}</em> exists in the location
			expected by <em>${violation.context}</em></li>
			<li>Change <em>${violation.context}</em> to no
			longer refer to <em>${violation.resourceName}</em></li>
			<li>Add <em>${violation.resourceName}</em> to the location expected.</li>
		</ul>
	</@suggested>
	<@location violation.hashCode(), locationText/>
	</li>
</#macro>

<#macro pageNumberValidationViolation violation locationText>
	<li class="${violation.severity}">Suspicious Page Number: Page number <em>${violation.pageNum}</em>
	<#if (violation.prevPage?? || violation.nextPage??)>is</#if>
	<#if violation.prevPage??>preceded by page <em>${violation.prevPage}</em></#if>
	<#if (violation.prevPage?? && violation.nextPage??)>and</#if>
	<#if violation.nextPage??>followed by page <em>${violation.nextPage}</em></#if>.
	This appears to be incorrect for the following reason: <em>${violation.msg}</em>.
	<@suggested violation.hashCode()>
		Page numbers vary in format from book to book, so this may not be a
		problem. Please compare to the original book, and verify that the page
		numbers do in fact change as highlighted above. Also, check the nesting
		of the pagenum elements in the XML file.
	</@suggested>
	<@location violation.hashCode(), locationText/>
	</li>
</#macro>

<#macro xmlValidationViolation violation locationText>
	<li class="${violation.severity}">Parsing Error at line ${violation.lineNumber}, column ${violation.columnNumber}: ${violation.message}.
	<@suggested violation.hashCode()>
		The above technical message is based upon the DTD validation. The DTD
		is the document which specifies how the file should be structured, and
		contains a series of rules. The above message explains how a particular
		rule has been violated by the file.
	</@suggested>
	<#if violation.previewContext??>
		<@preview violation.hashCode() violation.previewContext/>
	</#if>
	<@location violation.hashCode(), locationText/>
	</li>
</#macro>